﻿using System;
using System.Collections.Generic;
using System.Text;
using UIShell.OSGi;
using Pasys.PresentationCore;
using Pasys.PresentationCore.DockShell;
using DBExplorer.Builder;
using GeneratorLib.DB;
using UIShell.OSGi.Core.Service;

namespace DBExplorer
{
    public class Activator : IBundleActivator
    {
        //private
        private ICommandBusService commandBus;
        private IWorkspace workspace;

        private IExtensionManager _extensionManager;
        private ExtensionHooker _extensionHooker;

        public void Start(IBundleContext context)
        {
            var layoutContaint= BundleRuntime.Instance.GetFirstOrDefaultService<ILayoutViewProxyContainer>();
            IWorkspace workspace = layoutContaint.Workspace;
            var explorerMng = new DBExplorerManager();
            workspace.SmartPartClosing += explorerMng.WorkspaceSmartPartClosing;
            workspace.SmartPartActivated += explorerMng.Workspace_SmartPartActivated;


            context.AddService<IDBExplorerManager>(explorerMng);

            commandBus = BundleRuntime.Instance.GetFirstOrDefaultService<ICommandBusService>();
            commandBus.PublicCommand( CommandNames.ConnecteExplorer);

            _extensionManager = context.GetFirstOrDefaultService<IExtensionManager>();
            _extensionHooker = new ExtensionHooker(_extensionManager);


            context.AddService<IDBProviderManager>(new DBProviderManager());

            var provideManager = context.GetFirstOrDefaultService<IDBProviderManager>();

            var extensionChangeHandler = new ExtensionChangeHandler(new DBProviderBuilder(),
                              provider => provideManager.Register((IDBProvider)provider),
                              provider => provideManager.UnRegister((IDBProvider)provider));
            _extensionHooker.HookExtension(ExtensionPointNames.Explorer_DBProvider, extensionChangeHandler);

            extensionChangeHandler = new ExtensionChangeHandler(new DBTypeMapProviderBuilder(),
                              provider => GeneratorLib.DBTypeMapConfig.Instance.Register(((IDBTypeMapProvider)provider).MapName, ((IDBTypeMapProvider)provider).MapFilePath),
                              provider => GeneratorLib.DBTypeMapConfig.Instance.UnRegister(((IDBTypeMapProvider)provider).MapName));
            _extensionHooker.HookExtension(ExtensionPointNames.Explorer_DBTypeMapProvider, extensionChangeHandler);


        }


        public void Stop(IBundleContext context)
        {
            _extensionHooker.Dispose();

            //var workspace = BundleRuntime.Instance.GetFirstOrDefaultService<IWorkspace>();
            var explorerMng = (DBExplorerManager)BundleRuntime.Instance.GetFirstOrDefaultService<IDBExplorerManager>();

            workspace.SmartPartClosing -= explorerMng.WorkspaceSmartPartClosing;
            workspace.SmartPartActivated -= explorerMng.Workspace_SmartPartActivated;

        }
    }
}
